package org.jetnuke.dao.generic;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.entities.OsCounter;

public class GenericOsCounterDAO implements org.jetnuke.dao.OsCounterDAO {
	public int getTotalCount()throws Exception{
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("OsCounterModel.getTotalCount"));
		ResultSet rs = p.executeQuery();
		rs.next();
		int r= rs.getInt(1);
		rs.close();
		p.close();
		return r;
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.OsCounterModel#selectAll()
	 */
	public List selectAll() throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("OsCounterModel.selectAll"));
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}

	/**
	 * @param rs
	 * @return
	 */
	protected List processSelectAll(ResultSet rs)throws Exception {
		List list = new ArrayList();
		while (rs.next()) {
			OsCounter u = new OsCounter();
			fillOsCounterFromResultSet(u,rs);			
			list.add(u);
		}
		return list;
	}

	/**
	 * @param u
	 * @param rs
	 */
	protected void fillOsCounterFromResultSet(OsCounter u, ResultSet rs) throws Exception{
		u.setCOUNTER(rs.getInt("COUNTER"));
		u.setOID(rs.getString("OID"));
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.OsCounterModel#incCounter(java.lang.String)
	 */
	public void incCounter(String os) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("OsCounterModel.incCounter"));
		p.setString(1,os);
		p.executeUpdate();
		p.close();
		
	}
}
